# -*- coding: utf-8 -*-
# @Time    : 2020/6/20 上午12:04
# @Author  : caotian
# @FileName: convolution.py
# @Software: PyCharm
import matplotlib.pyplot as plt
import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D
from paddle.fluid.initializer import NumpyArrayInitializer

with fluid.dygraph.guard():
    w=np.array([1,0,-1],dtype='float32')
    w=w.reshape([1,1,1,3])
    conv=Conv2D(num_channels=1,num_filters=1,filter_size=[1,3],param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer(value=w)))
    img=np.ones([50,50],dtype='float32')
    img[:,30:]=0.
    x=img.reshape([1,1,50,50])
    x=fluid.dygraph.to_variable(x)
    y=conv(x)
    out=y.numpy()
f=plt.subplot(121)
f.set_title('input image',fontsize=15)
plt.imshow(img,cmap='gray')

f=plt.subplot(122)
f.set_title('output featuremap',fontsize=16)
plt.imshow(out.squeeze(),cmap='gray')
plt.show()

with fluid.dygraph.guard():
    print(conv.parameters())
    print(conv.parameters()[0].name,conv.parameters()[0].numpy())
    print(conv.parameters()[1].name,conv.parameters()[1].numpy())
